package cn.zlj.pethome.basic.service.impl;

import cn.zlj.pethome.basic.service.VerifyCodeService;
import cn.zlj.pethome.basic.config.BussinessException;
import cn.zlj.pethome.basic.contant.BaseConstants;
import cn.zlj.pethome.basic.dto.RegisterDto;
import cn.zlj.pethome.basic.util.StrUtils;
import cn.zlj.pethome.basic.util.VerifyCodeUtils;
import cn.zlj.pethome.user.domain.User;
import cn.zlj.pethome.user.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class VerifyCodeServiceImpl implements VerifyCodeService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;
    @Override
    public String verifyCode(String key) {
        if(StringUtils.isBlank(key)){
            throw new BussinessException("uuid异常");
        }
        String random = StrUtils.getRandomString(4);
        redisTemplate.opsForValue().set(key,random);
        VerifyCodeUtils verifyCodeUtils = new VerifyCodeUtils();
        String base64 = verifyCodeUtils.VerifyCode(100, 30,random );
        return base64;
    }

    @Override
    public void smsCode(RegisterDto registerDto) throws IOException {
        if(registerDto.getType().equals("binder")){
            //发送验证码
            SendMsg(registerDto,String.format(BaseConstants.VerifyCodeConstants.BUSINESS_BINDER_PREFIX, registerDto.getPhone()));
        }else {
            //判断是否为空
            //判断验证码是否一致
            VerifyimageCode(registerDto);
            SendMsg(registerDto,String.format(BaseConstants.VerifyCodeConstants.BUSINESS_REGISTER_PREFIX, registerDto.getPhone()));
        }

    }

    private void SendMsg(RegisterDto registerDto,String redisKey) {
        //判断手机号是否已经被注册
        User user=userMapper.findByPhone(registerDto.getPhone());
        if (user!=null){
            throw new BussinessException("已注册，请登录");
        }
        //创建验证码先为空，看redis中是否存在
        String smsCode = "";
        //判断验证码是否超时
        String redisSmsCode =(String) redisTemplate.opsForValue().get(redisKey);
        if(StringUtils.isBlank(redisSmsCode)){
            Long expire = redisTemplate.getExpire(redisKey, TimeUnit.MINUTES);
            if(expire>=4){
                throw new BussinessException("手机验证码超时请重发");
            }
        }else{
            smsCode=StrUtils.getRandomString(4);
            log.info("验证码为："+smsCode);
        }
        String redisValue=smsCode+":"+ registerDto.getPhone();
        //把验证码加入redis设置时间为4分钟
        redisTemplate.opsForValue().set(redisKey,redisValue,5, TimeUnit.MINUTES);
        //发送短信

        String msg="你好：你的验证码为"+smsCode+"5分钟内有效！";

        //SendMsgUtil.SendMsg(registerDto.getPhone(),msg);
    }

    private void VerifyimageCode(RegisterDto registerDto) {
        String imgCode =(String)redisTemplate.opsForValue().get(registerDto.getImageCodeKey());
        if (StringUtils.isBlank(imgCode)){
            throw new BussinessException("验证码为空");
        }
        if(!registerDto.getImageCodeValue().equals(imgCode)){
            throw new BussinessException("验证码错误");
        }
    }
}
